﻿// Infrastructure/CodeGeneration/EntityGenerator.cs

using System;
using System.Collections.Generic;
using System.Text;
using DatabaseManager.Core.Models;
using DatabaseManager.Utilities;

namespace DatabaseManager.Infrastructure.CodeGeneration
{
    /// <summary>
    /// 实体类生成器的实现类，用于根据数据库表结构生成对应的C#实体类代码
    /// 实现了IEntityGenerator接口，负责将表和列信息转换为实体类代码
    /// </summary>
    public class EntityGenerator : IEntityGenerator
    {
        /// <summary>
        /// 根据表信息和列信息生成实体类代码
        /// </summary>
        /// <param name="table">表结构信息对象，包含表名等元数据</param>
        /// <param name="columns">列信息集合，包含列名、数据类型等详细信息</param>
        /// <returns>生成的实体类完整代码字符串</returns>
        /// <exception cref="ArgumentNullException">当table或columns为null时抛出</exception>
        public string GenerateEntityClass(TableInfo table, List<ColumnInfo> columns)
        {
            // 参数验证，确保输入有效
            if (table == null)
                throw new ArgumentNullException(nameof(table));
            if (columns == null)
                throw new ArgumentNullException(nameof(columns));

            // 将表名转换为PascalCase作为类名
            var className = NameConverter.ToPascalCase(table.Name);
            var properties = new List<string>();

            // 遍历所有列，生成对应的属性代码
            foreach (var column in columns)
            {
                // 将SQL数据类型映射为C#数据类型
                string csharpType = MapSqlTypeToCSharp(column.DataType);
                // 将列名转换为PascalCase作为属性名
                string propertyName = NameConverter.ToPascalCase(column.Name);
                // 添加自动属性定义
                properties.Add($"public {csharpType} {propertyName} {{ get; set; }}");
            }

            // 使用StringBuilder高效拼接代码字符串
            var codeBuilder = new StringBuilder();
            codeBuilder.AppendLine("using System;");
            codeBuilder.AppendLine();
            codeBuilder.AppendLine("namespace DatabaseManager.Models.Entities");
            codeBuilder.AppendLine("{");
            // 添加SqlSugar表映射特性
            codeBuilder.AppendLine($"    [SqlSugar.SugarTable(\"{table.Name}\")]");
            // 生成类定义
            codeBuilder.AppendLine($"    public class {className}");
            codeBuilder.AppendLine("    {");

            // 添加所有属性
            foreach (var property in properties)
            {
                codeBuilder.AppendLine($"        {property}");
            }

            codeBuilder.AppendLine("    }");
            codeBuilder.AppendLine("}");

            return codeBuilder.ToString();
        }

        /// <summary>
        /// 将SQL数据类型映射为对应的C#数据类型
        /// </summary>
        /// <param name="sqlType">SQL数据类型字符串</param>
        /// <returns>对应的C#数据类型字符串</returns>
        private string MapSqlTypeToCSharp(string sqlType)
        {
            // 处理空值情况
            if (string.IsNullOrEmpty(sqlType))
                return "object";

            // 转换为小写便于统一判断
            var lowerSqlType = sqlType.ToLower();

            // 根据SQL类型映射到C#类型
            switch (lowerSqlType)
            {
                case "int":
                case "integer":
                    return "int";
                case "bigint":
                    return "long";
                case "varchar":
                case "nvarchar":
                case "text":
                    return "string";
                case "datetime":
                case "date":
                    return "DateTime";
                case "bit":
                    return "bool";
                case "decimal":
                case "numeric":
                    return "decimal";
                case "float":
                    return "double";
                case "guid":
                case "uniqueidentifier":
                    return "Guid";
                // 未知类型默认映射为object
                default:
                    return "object";
            }
        }
    }
}
